import yaml
import requests
import re
import os
import time

# r = requests.get("https://raw.githubusercontent.com/asd920/Auto-jd/main/serverless.yml", stream = True)
#
# with open("serverless.yml", "wb") as f:
#     f.write(r.content)


time_now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
TEXT = "# " + time_now + "\n"

try:
    from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
    from yaml import Loader, Dumper

yaml_file = open("../serverless.yml", "r")
data = yaml.load(yaml_file, Loader=yaml.FullLoader)


inputs = data['inputs']
events = inputs['events']
for event in events:
    timer = event['timer']
    parameters = timer['parameters']
    print(parameters)
    cronExpression = parameters['cronExpression']
    print(cronExpression[2:-1])
    argument = parameters['argument']
    k = re.split("&", argument)
    for a_k in k:
        TEXT = TEXT + cronExpression[2:-1] + "node /scripts/"+a_k + ".js >> /scripts/logs/" + a_k + ".log\n"

with open('crontab_list.sh' ,"w") as f:
    f.write(TEXT)

